package com.example.traning.jdbc;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @author lei yu
 * @since 2025/04/21
 */
public class LoginV1 {

    public static void main(String[] args) throws Exception {
        // 1，在控制台录入用户名，去数据库查询是否存在记录；如果存在，则登录成功；否则，登录失败
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String name = scanner.next();
        // 2,获取连接
        Connection conn = DbUtils.getConnection();
        // 3,获取执行SQL语句的对象
        Statement statement = conn.createStatement();
        String sql = String.format("select * from emp where ename = '%s'", name);
        System.out.println("sql = " + sql);
        ResultSet rs = statement.executeQuery(sql);
        // 4. 处理结果，只要有记录，可以判断登录成功
        if (rs.next()) {
            System.out.println("登录成功");
        } else {
            System.out.println("登录失败");
        }
        DbUtils.closeAll(conn, statement, rs);

    }
}
